package com.pipayshop.controller;

import com.pipayshop.domain.dto.ShopCommodityEvaluateAddDto;
import com.pipayshop.domain.vo.PageVo;
import com.pipayshop.entity.vo.ResponseVO;
import com.pipayshop.exception.BusinessException;
import com.pipayshop.service.OnlineStoreShopCommodityEvaluateService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

/**
 * @author xiaoqiu
 * @date 2024/9/23 10:27
 * @description 网店商品评价接口
 */
@RestController
@RequestMapping("/pipayshopapi/evaluate")
@Slf4j
public class OnlineStoreShopCommodityEvaluateController {
    @Autowired
    private OnlineStoreShopCommodityEvaluateService shopCommodityEvaluateService;


    @PostMapping("addItemEvaluates")
    @ApiOperation("新增网店商品的评价")
    @CacheEvict(value = "getShopCommodityEvaluates", key = "'getShopCommodityEvaluates'+#shopCommodityEvaluateAddDto.commodityId")
    public ResponseVO<String> addItemEvaluates(@RequestBody ShopCommodityEvaluateAddDto shopCommodityEvaluateAddDto){
        try {
            boolean result = shopCommodityEvaluateService.addItemEvaluates(shopCommodityEvaluateAddDto);
            if (!result){
                throw new Exception();
            }
            return ResponseVO.getSuccessResponseVo("add_product_review_success");
        }catch (BusinessException e) {
            throw new BusinessException(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("add_product_review_failed");
        }
    }

    @GetMapping("getShopCommodityEvaluates/{commodityId}/{currentPage}/{limit}")
    @ApiOperation("获取网店商品评价列表")
    @CacheEvict(value = "getShopCommodityEvaluates", allEntries = true )
    public ResponseVO getItemCommodityEvaluates(@PathVariable String commodityId, @PathVariable Integer currentPage, @PathVariable Integer limit){
        try{
            PageVo itemCommodityEvaluates = shopCommodityEvaluateService.getItemCommodityEvaluates(commodityId,currentPage,limit);
            return ResponseVO.getSuccessResponseVo(itemCommodityEvaluates);
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("get_product_review_list_failed");
        }
    }

    @PostMapping("deleteShopEvaluates/{evaluateId}/{userId}")
    @ApiOperation("根据评价Id删除网店商品自己评价")
    @CacheEvict(value = "getShopCommodityEvaluates", allEntries = true )
    public ResponseVO<String> deleteItemEvaluates(@PathVariable("evaluateId") String evaluateId, @PathVariable("userId") String userId){
        try {
            boolean result = shopCommodityEvaluateService.deleteItemEvaluates(evaluateId, userId);
            if (!result){
                throw new Exception();
            }
            return ResponseVO.getSuccessResponseVo("delete_review_by_id_success");
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("delete_review_by_id_failed");
        }
    }


}
